import requests
import re
import os

def mkdir(path):
    # 创建本地路径
    if not os.path.exists(path):
        os.makedirs(path)
        print(path, "已创建")
    else:
    	return

def get_page(url):
	headers = {'User-Agent':'Mozilla/5.0(Macintosh;U;IntelMacOSX10_6_8;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/5.1Safari/534.50'}
	try:
		response = requests.get(url ,headers=headers)
		response.encoding = response.apparent_encoding
		if response.status_code == 200:
			return response.text
		return None
	except:
		print('没有请求到页面！')

def get_picture_url(html):
	pattern = re.compile('<img accessUrl="([\s\S]*?)"')
	results = re.findall(pattern ,html)
	if results:
		for item in results:
			yield item
	else:
		print('re匹配失败！')

def write2File(filename, item):
	with open(filename, 'a', encoding='utf-8') as f:
		f.write(item +'\r\n')

def crawPage():
	html = get_page(url)
	for item in get_picture_url(html):
		write2File(filename, item)

if __name__ == '__main__':
	url = 'http://pic.haibao.com/hotimage/'
	outputPath = 'D:\PictrueFile'
	mkdir(outputPath)
	filename = outputPath + '/图片链接.txt'
	crawPage()
